package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.zhijian.medical.dao.local.ExperimentProcessDetailMapper;
import com.zhijian.medical.entity.dto.YgpcrSjpzqKnbSjDto;
import com.zhijian.medical.entity.po.ExperimentProcess;
import com.zhijian.medical.entity.po.ExperimentProcessDetail;
import com.zhijian.medical.entity.vo.request.ExperimentProcessDetailSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.ExperimentProcessDetailSearchVo;
import com.zhijian.medical.entity.vo.request.ExperimentProcessSampleSearchVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessDetailVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessSampleHstqVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessSampleVo;
import com.zhijian.medical.service.ExperimentProcessDetailService;
import com.zhijian.medical.service.ExperimentProcessSampleHstqService;
import com.zhijian.medical.service.ExperimentProcessSampleService;
import com.zhijian.medical.service.ExperimentProcessService;
import com.zhijian.medical.util.JsonUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 实验流程-实验详情业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-04-07 22:44:12
 */
@Slf4j
@Service
public class ExperimentProcessDetailServiceImpl implements ExperimentProcessDetailService {

    @Autowired
    private ExperimentProcessDetailMapper mapper;

    @Autowired
    private ExperimentProcessService experimentProcessService;

    @Autowired
    private ExperimentProcessSampleService experimentProcessSampleService;

    @Autowired
    private ExperimentProcessSampleHstqService experimentProcessSampleHstqService;

    @Override
    public ExperimentProcessDetail selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExperimentProcessDetailVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(ExperimentProcessDetail experimentProcessDetail) {
        Date now = new Date();
        experimentProcessDetail.setCreateTime(now);
        experimentProcessDetail.setUpdateTime(now);
        return mapper.insert(experimentProcessDetail) > 0 ? experimentProcessDetail.getId() : 0L;
    }

    @Override
    public boolean update(ExperimentProcessDetail experimentProcessDetail) {
        Date now = new Date();
        experimentProcessDetail.setUpdateTime(now);
        return mapper.updateByPrimaryKey(experimentProcessDetail) != 0;
    }

    @Override
    public boolean updateByExperimentProcessId(ExperimentProcessDetailSaveOrUpdateVo record) {
        return mapper.updateByExperimentProcessId(record) != 0;
    }

    @Override
    public boolean updateDate(Long experimentProcessId, String date) {
        ExperimentProcessDetailVo experimentProcessDetailVo = this.selectVoByExperimentProcessId(experimentProcessId);
        if (Objects.nonNull(experimentProcessDetailVo)) {
            return this.updateByExperimentProcessId(ExperimentProcessDetailSaveOrUpdateVo.builder()
                    .experimentProcessId(experimentProcessId)
                    .date(date)
                    .build());
        } else {
            return this.insert(ExperimentProcessDetail.builder()
                    .experimentProcessId(experimentProcessId)
                    .date(DateUtil.parseDate(date))
                    .build()) != 0;
        }
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean saveOrUpdate(ExperimentProcessDetailSaveOrUpdateVo experimentProcessDetailSaveOrUpdateVo) {
        Integer count = this.count(ExperimentProcessDetailSearchVo.builder()
                .experimentProcessId(experimentProcessDetailSaveOrUpdateVo.getExperimentProcessId())
                .build());
        List<YgpcrSjpzqKnbSjDto> sjpzqSjDataList = experimentProcessDetailSaveOrUpdateVo.getSjpzqSjDataList();
        if (CollectionUtil.isNotEmpty(sjpzqSjDataList)) {
            experimentProcessDetailSaveOrUpdateVo.setSjpzqSjData(JsonUtil.toString(sjpzqSjDataList));
        }
        if (count > 0) {
            this.updateByExperimentProcessId(experimentProcessDetailSaveOrUpdateVo);
        } else {
            ExperimentProcessDetail experimentProcessDetail = new ExperimentProcessDetail();
            BeanUtils.copyProperties(experimentProcessDetailSaveOrUpdateVo, experimentProcessDetail, "date");
            experimentProcessDetail.setDate(DateUtil.parseDate(experimentProcessDetailSaveOrUpdateVo.getDate()));
            this.insert(experimentProcessDetail);
        }
        //修改实验流程的状态
        experimentProcessService.updateByPrimaryKeySelective(ExperimentProcess.builder()
                .id(experimentProcessDetailSaveOrUpdateVo.getExperimentProcessId())
                .status(experimentProcessDetailSaveOrUpdateVo.getStatus())
                .updateTime(new Date())
                .build());
        return true;
    }


    @Override
    public boolean updateByPrimaryKeySelective(ExperimentProcessDetail experimentProcessDetail) {
        return mapper.updateByPrimaryKeySelective(experimentProcessDetail) != 0;
    }

    @Override
    public Integer count(ExperimentProcessDetailSearchVo experimentProcessDetailSearchVo) {
        return mapper.count(experimentProcessDetailSearchVo);
    }

    @Override
    public List<ExperimentProcessDetailVo> selectList(ExperimentProcessDetailSearchVo experimentProcessDetailSearchVo) {
        return mapper.selectList(experimentProcessDetailSearchVo);
    }

    @Override
    public Page<ExperimentProcessDetailVo> selectPage(ExperimentProcessDetailSearchVo experimentProcessDetailSearchVo) {
        //先直接查总数
        int count = count(experimentProcessDetailSearchVo);
        //分页
        Page<ExperimentProcessDetailVo> page = new Page<>(true, count, experimentProcessDetailSearchVo.getPageNum(), experimentProcessDetailSearchVo.getPageSize());
        experimentProcessDetailSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExperimentProcessDetailVo> responseVos = this.selectList(experimentProcessDetailSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public ExperimentProcessDetailVo selectVoByExperimentProcessId(Long experimentProcessId) {
        ExperimentProcessDetailVo experimentProcessDetailVo = mapper.selectVoByExperimentProcessId(experimentProcessId);
        if (Objects.nonNull(experimentProcessDetailVo)) {
            ExperimentProcess experimentProcess = experimentProcessService.selectById(experimentProcessId);
            experimentProcessDetailVo.setStatus(experimentProcess.getStatus());
            String sjpzqSjData = experimentProcessDetailVo.getSjpzqSjData();
            if (StringUtils.isNotBlank(sjpzqSjData)) {
                //试剂数据列表
                experimentProcessDetailVo.setSjpzqSjDataList(JsonUtil.toArray(sjpzqSjData, YgpcrSjpzqKnbSjDto.class));
            }
            return experimentProcessDetailVo;
        }
        //如果查不到，就去查这个荧光PCR实验的样本，从样本里找对应的准备区数据
        List<ExperimentProcessSampleVo> experimentProcessSampleVos = experimentProcessSampleService.selectList(ExperimentProcessSampleSearchVo.builder()
                .experimentProcessId(experimentProcessId)
                .build());
        if (CollectionUtil.isEmpty(experimentProcessSampleVos)) {
            return null;
        }
        Long hstqId = experimentProcessSampleVos.get(0).getHtstqId();
        if (Objects.isNull(hstqId)) {
            return null;
        }
        ExperimentProcessSampleHstqVo experimentProcessSampleHstqVo = experimentProcessSampleHstqService.selectVoById(hstqId);
        if (Objects.isNull(experimentProcessSampleHstqVo)) {
            return null;
        }
        ExperimentProcessDetailVo result = new ExperimentProcessDetailVo();
        //操作者
        result.setHszbqczz(experimentProcessSampleHstqVo.getCzz());
        //实验室台面清洁(75%乙醇)
        result.setHszbqhstqsyqsystmqj(experimentProcessSampleHstqVo.getSyqsystmqj());
        //试剂在有效期内
        result.setHszbqhstqhstqsjsjzyxqn(experimentProcessSampleHstqVo.getSjzyxqn());
        //受检组织的刮取及消化-过程有无异常
        result.setHszbqhstqsjzzdgqjxhgcywyc(experimentProcessSampleHstqVo.getSjzzdgqjxhgczywyc());
        //受检组织的刮取及消化处理方式
        result.setHszbqhstqsjzzdgqjxhclfs(experimentProcessSampleHstqVo.getSjzzdgqjxhgczclfs());
        //核酸提取过程有无异常
        result.setHszbqhstqhstqgcgcywyc(experimentProcessSampleHstqVo.getHstqgcywyc());
        //核酸提取过程处理方式
        result.setHszbqhstqhstqgcclfs(experimentProcessSampleHstqVo.getHstqgcclfs());
        //台式高速离心机
        result.setHszbqhstqyqsbsyqktsgslxj(experimentProcessSampleHstqVo.getTsgsldlxj());
        //震荡机
        result.setHszbqhstqyqsbsyqkzdq(experimentProcessSampleHstqVo.getZdq());
        //生物安全柜
        result.setHszbqhstqyqsbsyqkswaqg(experimentProcessSampleHstqVo.getSwaqg());
        //恒温金属浴
        result.setHszbqhstqyqsbsyqkhwjsg(experimentProcessSampleHstqVo.getHwjsy());
        //实验后-清洁及紫外照射实验工作台面
        result.setHszbqhstqsyhqjjzwzssygztm(experimentProcessSampleHstqVo.getSyhqjjzwzssygztm());
        //实验后-进行实验区紫外线（固定或移动）照射
        result.setHszbqhstqsyhjxsyqzwxzs(experimentProcessSampleHstqVo.getSyhjxsyqzwxzs());
        //实验后-处理实验废弃物
        result.setHszbqhstqsyhclsyfqw(experimentProcessSampleHstqVo.getSyhclsyfqw());
        return result;
    }

}